package com.qfedu.controller;

import com.github.pagehelper.Page;
import com.qfedu.common.JsonResult;
import com.qfedu.entity.Exercises;
import com.qfedu.service.ExercisesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName: backStage
 * author: CGY
 * time: 2020/10/7 10:06
 * description:
 */
@Controller
@RequestMapping("/exer")
public class ExercisesController {
    @Autowired
    private ExercisesService service;

    /**
     * 添加功能
     * @param exercises
     * @return
     */
    @RequestMapping("/add.do")
    @ResponseBody
    public JsonResult add(Exercises exercises){
        service.addExercises(exercises);
        return new JsonResult(1,"添加成功！");
    }

    @ResponseBody
    @RequestMapping("/select.do")
    public Map<String,Object> listExerciseInfo(Integer page,Integer limit){
        List<Exercises> list = service.selectExercises(page,limit);
        System.out.println("list = " + list);
        //获取总记录数
        long total = ((Page) list).getTotal();

        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", total);
        map.put("data", list);
        System.out.println(map);
        return map;
    }

    @RequestMapping("/delete.do")
    @ResponseBody
    public JsonResult deleteByEid(int id) {
        service.deleteByEid(id);
        return new JsonResult(1, "删除成功");
    }
}
